#ifndef  _RW_xmlNodeHelper_H 
#define  _RW_xmlNodeHelper_H 

#include <stdio.h>
#include "libxml/parser.h"
#include "libxml/tree.h"
#include <map>
#include <vector>
using namespace std;

struct ptrCmp
{
	bool operator()( const char * s1, const char * s2 ) const
	{
		return strcmp( s1, s2 ) < 0;
	}
};

class XmlNodeMap
{
public:
	map<char *  ,xmlNodePtr , ptrCmp> m_params_map;
	vector<char*> m_nodeName_list;
	
	static XmlNodeMap readFromXmlNode(xmlNodePtr  xmlNode)
	{
		XmlNodeMap node;
		node.readChildrenKV2Map(xmlNode);
		return node;
	}

	vector <char*> getNodeNameList()
	{
		return m_nodeName_list;
	}

	 map<char *    ,xmlNodePtr , ptrCmp> readChildrenKV2Map(xmlNodePtr  init_node)
		 {

			 for (init_node= init_node->children; init_node!=NULL ;init_node = init_node->next)
			 {
				 m_params_map.insert(make_pair((char*)init_node->name, init_node));
				 m_nodeName_list.push_back((char*)init_node->name);
			 }
			 return m_params_map;
		 }

		 bool hasNode( char * nodeName  )
		 {
			 return  ! (m_params_map.find(nodeName) ==m_params_map.end());
		 }

		 bool hasAttribute(char * nodeName   ,char * attrName)
		 {
			 if (! hasNode( nodeName)) return false;
			 if (xmlGetProp(m_params_map.at(nodeName) , (xmlChar*)attrName) ==NULL) return false;
			 return true;
		 }


		 char * getNodeAttribute(char * nodeName ,   char *  attrName)
		 {
			 return  (char*)xmlGetProp(m_params_map.at(nodeName) ,(xmlChar*)attrName);
		 }

		 int getNodeAttributeInt( char * nodeName ,char * attrName)
		 {
			 return atoi((char*)getNodeAttribute( nodeName , attrName));
		 }

		 float getNodeAttributeFloat(char * nodeName ,char * attrName)
		 {
			 return atof((char*)getNodeAttribute( nodeName , attrName));
		 }

		 char * getNodeContent( char * nodeName)
		 {
			 return (char*)xmlNodeGetContent(m_params_map.at(nodeName));

		 }

		 int getNodeContentInt( char * nodeName )
		 {
			 return atoi((char*)getNodeContent( nodeName));
		 }

		 float getNodeContentFloat( char * nodeName )
		 {
			 return atof((char*)getNodeContent( nodeName));
		 }

		 bool getNodeContentBool( char * xmlnodeName ) 
		 {
			  char * val = getNodeContent(xmlnodeName);
			  if (xmlStrcmp((xmlChar*)val , (xmlChar*)"true")==0)
			  {
				  return true;
			  }
			  else if (xmlStrcmp((xmlChar*)val ,(xmlChar*) "false")==0)
			  {
				  return false;
			  }
			  return false;
		 }


};



#endif
